package crp.services.api.graphql.loaders;

import com.qwlabs.lang.C2;
import crp.core.space.app.SpaceService;
import crp.core.space.domain.models.LocationEntity;
import crp.services.api.graphql.models.LocationPayload;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Source;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;

@GraphQLApi
@RequestScoped
public class LocationPayloadLoader {
    private final SpaceService spaceService;

    @Inject
    public LocationPayloadLoader(SpaceService spaceService) {
        this.spaceService = spaceService;
    }

    @NotNull
    public List<List<LocationPayload>> children(@Source List<LocationPayload> locations) {
        Set<String> parentLocationIds = C2.set(locations, LocationPayload::getId);
        return null;
//        Map<String, List<LocationPayload>> childrenMapping = C2.listMap(
//                locationRepository.findByParentIds(parentLocationIds),
//                LocationEntity::getParentId,
//                LocationPayload::of);
//        return locations.stream()
//                .map(LocationPayload::getId)
//                .map(parentId -> childrenMapping.getOrDefault(parentId, List.of()))
//                .collect(Collectors.toList());
    }
//    public List<LocationPayload> parent(@Source List<LocationPayload> locations) {
//        Set<String> parentLocationIds = C2.set(locations, LocationPayload::getParentId);
//        Map<String, LocationPayload> mapping = C2.map(
//                spaceService.locationSearcher().findByIds(parentLocationIds),
//                LocationEntity::getId,
//                LocationPayload::of);
//        return C2.list(locations, location -> mapping.get(location.getParentId()));
//    }
}
